package com.example.day_day_up.servlet;

import com.example.day_day_up.entity.constants.Constants;
import com.example.day_day_up.entity.dto.EditUserDTO;
import com.example.day_day_up.entity.po.User;
import com.example.day_day_up.enums.OperateEnum;
import com.example.day_day_up.service.UserService;
import com.example.day_day_up.utils.JsonUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private UserService userService;

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext servletContext = getServletContext();
        userService = (UserService) servletContext.getAttribute("userService");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<EditUserDTO> userList = new ArrayList<>();
        HttpSession session = req.getSession();

        if (req.getParameter(Constants.CURRENT_PAGE) == null) {
            // 首次进入userManager页面
            session.setAttribute(Constants.CURRENT_PAGE, 1);
            userList = userService.getUserInPage(1);
            // 得到总页数
            int totalPage = userService.getPageCount();
            session.setAttribute(Constants.TOTAL_PAGE, totalPage);
        } else {
            int currentPage = Integer.parseInt(req.getParameter(Constants.CURRENT_PAGE));
            session.setAttribute(Constants.CURRENT_PAGE, currentPage);
            userList = userService.getUserInPage(currentPage);
        }
        session.setAttribute(Constants.USERS_KEY, userList);
        JsonUtils.writeSuccessResponse(resp, userList);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        List<EditUserDTO> userList = new ArrayList<>();
        int currentPage = 1;

        int type = Integer.parseInt(req.getParameter("type"));
        if (type == OperateEnum.ADD.getCode()) {
            // 添加用户
            String id = req.getParameter("id");
            String password = req.getParameter("password");
            int role = Integer.parseInt(req.getParameter("role"));
            userService.addUser(id, password, role, resp);

        } else if (type == OperateEnum.UPDATE.getCode()) {
            // 更新用户
            String id = req.getParameter("id");
            String nickname = req.getParameter("nickname");
            boolean status = false;
            String role = req.getParameter("role");
            if (role.equals(Constants.ROLE_MANAGER)) {
                status = true;
            }
            User updateUser = new User();
            updateUser.setId(id);
            updateUser.setNickname(nickname);
            updateUser.setStatus(status);
            userService.updateUser(updateUser, resp);

            // 保持当前页
            currentPage = (int) session.getAttribute(Constants.CURRENT_PAGE);
        }

        userList = userService.getUserInPage(currentPage);
        session.setAttribute(Constants.USERS_KEY, userList);
        session.setAttribute(Constants.CURRENT_PAGE, currentPage);
    }
}
